<template>
	<el-dialog v-model="visible" :title="!dataForm.id ? '新增' : '修改'" :close-on-click-modal="false" draggable width="80%">
		<el-form ref="dataFormRef" :model="dataForm" :rules="dataRules" label-width="80px" @keyup.enter="submitHandle()">
			<el-form-item label="患者姓名" prop="patientName">
				<el-input v-model="dataForm.patientName" @click="addOrUpdateHandle()"></el-input>
			</el-form-item>
			<el-form-item label="入院科室" prop="deptName">
				<el-input v-model="dataForm.deptName" @click="addOrUpdateHandle()"></el-input>
			</el-form-item>
			<el-form-item label="主管护士" prop="nurseName">
				<el-input v-model="dataForm.nurseName" @click="addUserHandle()"></el-input>
			</el-form-item>
			<el-form-item label="床位" prop="bedName">
				<el-input v-model="dataForm.bedName"></el-input>
			</el-form-item>
		</el-form>
		<div style="width:100%;height:300px;float:left; margin-top: 10px;margin-left: 10px;
			margin-bottom: 100px;">
			<WangEditor v-model="dataForm.houseDia"  placeholder="请输入住院诊断..."></WangEditor>
		</div>
		<template #footer>
			<el-button @click="visible = false">取消</el-button>
			<el-button type="primary" @click="submitHandle()">确定</el-button>
		</template>
	</el-dialog>
	<!-- 弹窗, 新增 / 修改 -->
	<datalistUser ref="datalistUserRef" @fatherMethod="fatherMethod"></datalistUser>
	<!-- 弹窗, 新增 / 修改 -->
	<listUser ref="listUserRef" @fatherUserMethod="fatherUserMethod"></listUser>
</template>

<script setup lang="ts">
import { reactive, ref } from 'vue'
import { ElMessage } from 'element-plus/es'
import { useHousePatientApi, useHousePatientSubmitApi } from '@/api/sys/housepatient'
import WangEditor from '@/components/wang-editor/index.vue'
import datalistUser from './datalist.vue'
import listUser from './nurselist.vue'
import {usePatientSubmitApi } from '@/api/sys/patient'
const emit = defineEmits(['refreshDataList'])

const visible = ref(false)
const dataFormRef = ref()

const dataForm = reactive({
	id: '',
	patientId: '',
	patientName: '',
	houseDia: '',
	deptId:'',
	deptName:'',
	nurseId:'',
	nurseName:'',
	bedName:'',
	orgId:'',
	patientNum:0,
	costMoney:0,
})

const datalistUserRef = ref()
const addOrUpdateHandle = () => {
	datalistUserRef.value.init();
}
const fatherMethod=(orgTit:any,org:any)=>{
	dataForm.deptName=orgTit;
	dataForm.deptId=org;
}

const listUserRef = ref()
const addUserHandle = () => {
	listUserRef.value.init();
}

const fatherUserMethod=(realName:any,uid:any)=>{
	dataForm.nurseName=realName;
	dataForm.nurseId=uid;
}
const init = (obj: any) => {
	visible.value = true
	dataForm.id = ''
	let orgid:any=localStorage.getItem('org');
	dataForm.orgId=orgid;
	dataForm.patientId=obj.uid;
	dataForm.patientName=obj.userName;
	dataForm.patientNum=obj.id;
}

const dataRules = ref({
	patientName: [{ required: true, message: '必填项不能为空', trigger: 'blur' }],
	deptName: [{ required: true, message: '必填项不能为空', trigger: 'blur' }],
	nurseName: [{ required: true, message: '必填项不能为空', trigger: 'blur' }],
	bedName: [{ required: true, message: '必填项不能为空', trigger: 'blur' }]
})

// 表单提交
const submitHandle = () => {
	dataFormRef.value.validate((valid: boolean) => {
		if (!valid) {
			return false
		}

		useHousePatientSubmitApi(dataForm).then(() => {
			
			let param={id:dataForm.patientNum,status:2,costMoney:0}
			usePatientSubmitApi(param).then(() => {
				ElMessage.success({
					message: '操作成功',
					duration: 500,
					onClose: () => {
						visible.value = false
						emit('refreshDataList')
					}
				})
			})
		})
	})
}

defineExpose({
	init
})
</script>
